<template>
  <div>
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Import')"
      >导入</el-button
    >
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Export')"
      >导入</el-button
    >
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Upload')"
      >上传</el-button
    >
    <el-button v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Audit')"
      >审核</el-button
    >
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="Id" label="角色Id"> </el-table-column>
      <el-table-column prop="Name" label="角色名称"> </el-table-column>
      <el-table-column prop="Id" label="操作">
        <template slot-scope="data">
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="RoleDel(data.row.Id)"
            circle
            v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Delete')"
            >删除</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="RoleUpd(data.row.Id)"
            v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Update')"
            circle
            >修改</el-button
          >
          <el-button
            @click="RolePerDia(data.row.Id)"
            circle
            v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Add')"
            >权限分配</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-button
      type="info"
      @click="OpenRoleAddDia"
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Add')"
      >添加信息</el-button
    >
    <el-dialog title="角色添加" :visible.sync="dialogTableVisibleRoleAdd">
      <RoleAddView @CloseRoleAddDia="CloseRoleAddDia"></RoleAddView>
    </el-dialog>
    <el-dialog
      title="角色修改"
      v-if="dialogTableVisibleRoleUpd"
      :visible.sync="dialogTableVisibleRoleUpd"
    >
      <RoleUpdView :Rid="Rid" @CloseRoleUpdDia="CloseRoleUpdDia"></RoleUpdView>
    </el-dialog>
    <el-dialog
      title="权限分配"
      v-if="dialogTableVisibleRolePerAdd"
      :visible.sync="dialogTableVisibleRolePerAdd"
    >
      <RolePerAddView
        :PRid="PRid"
        @CloseRolePerAdd="CloseRolePerAdd"
      ></RolePerAddView>
    </el-dialog>
  </div>
</template>

<script>
import RoleAddView from "../../components/RoleComponet/RoleAddComp.vue";
import RoleUpdView from "../../components/RoleComponet/RoleUpdComp.vue";
import RolePerAddView from "../../components/RoleComponet/RolePerAddComp.vue";
export default {
  components: {
    RoleAddView,
    RoleUpdView,
    RolePerAddView,
  },
  data() {
    return {
      dialogTableVisibleRoleAdd: false,
      dialogTableVisibleRoleUpd: false,
      dialogTableVisibleRolePerAdd: false,
      tableData: [],
      Rid: 0,
      PRid: 0,
      PerButton: "",
    };
  },
  created() {
    this.GetRoleInfo();
    this.getRolePerM();
  },
  methods: {
    CloseRolePerAdd() {
      this.dialogTableVisibleRolePerAdd = false;
      this.GetRoleInfo();
    },
    RolePerDia(Id) {
      this.PRid = Id;
      this.dialogTableVisibleRolePerAdd = true;
    },
    RoleDel(Id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete("http://localhost:5255/api/Role/RoleDel?Id=" + Id)
            .then((res) => {
              if (res.data > 0) {
                this.$message.success("删除成功");
                this.GetRoleInfo();
              } else {
                this.$message.error("删除失败");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    OpenRoleAddDia() {
      this.dialogTableVisibleRoleAdd = true;
    },
    CloseRoleAddDia() {
      this.dialogTableVisibleRoleAdd = false;
      this.GetRoleInfo();
    },
    CloseRoleUpdDia() {
      this.dialogTableVisibleRoleUpd = false;
      this.GetRoleInfo();
    },
    RoleUpd(Id) {
      this.Rid = Id;
      this.dialogTableVisibleRoleUpd = true;
    },
    GetRoleInfo() {
      this.axios.get("http://localhost:5255/api/Role/GetRole").then((res) => {
        this.tableData = res.data.Data;
      });
    },
    getRolePerM() {
      this.axios
        .get("http://localhost:5255/api/User/GetRolePeimiss", {
          params: {
            UserId: sessionStorage.UserId,
            PermId: this.$route.query.Id,
          },
        })
        .then((res) => {
          this.PerButton = res.data.Data;
        });
    },
  },
};
</script>

<style>
</style>